package com.sense.kukoo.biz_me.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_me.login.core.FacebookLoginManager
import com.sense.kukoo.biz_me.login.core.GoogleLoginManager
import com.sense.kukoo.biz_me.login.core.LineLoginManager
import com.sense.kukoo.common.event.LogoutEvent
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.biz.ChannelContentBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.repo.http.bean.UserInfoBean
import com.sense.kukoo.lib_logger.LoggerManager
import org.greenrobot.eventbus.EventBus

class MeViewModel : BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    private var mPageIndex = 2
    private val mPageSize = 20
    private var mIsRefreshData = true

    //val action_toRefreshUser: MutableLiveData<Boolean> = MutableLiveData()
    val action_toSettingsPage: MutableLiveData<Boolean> = MutableLiveData()
    val action_toCreatorPage: MutableLiveData<Boolean> = MutableLiveData()
    val action_toCreatorUploadPage: MutableLiveData<Boolean> = MutableLiveData()
    val action_toMyHistoryPage: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishRefres: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val action_network_error: MutableLiveData<Boolean> = MutableLiveData()
    val data_channelContent: MutableLiveData<ArrayList<ChannelContentBean>> = MutableLiveData()
    val data_enableLoadMore:MutableLiveData<Boolean> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun onRefreshData(){
        mIsRefreshData = true
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            action_network_error.postValue(true)
        }
        request(onError) {
            val data = arrayListOf<ChannelContentBean>()
            //
            val userInfo = Repo.http.getUserInfo(0L)
            if (userInfo.isSuccess()) {
                val user: UserInfoBean = userInfo.data!!
                Repo.user.setUser(user)
                //
                data.add(ChannelContentBean(ChannelContentBean.TYPE_TITLE, user, null))
            } else {
                if (userInfo.code==401) {
                    //token超时，回到未登陆
                    EventBus.getDefault().post(LogoutEvent())
                }
                LoggerManager.e(TAG, "获取用户信息失败")
                return@request
            }
            //
            val userContent = Repo.http.getUserContent(1, mPageSize, 0L)
            if(userContent.isSuccess()){
                mPageIndex = 2
                val userContentData:List<IndexContentBean> = userContent.data!!
                if(userContentData.size == 0){
                    data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_EMPTY, null, null))
                    data_enableLoadMore.postValue(false)
                } else {
                    userContentData.forEach {
                        if(IndexContentBean.STATE_NORMAL == it.state){
                            data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_NORMAL, null, it))
                        } else {
                            data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_HINT, null, it))
                        }
                    }
                    if(userContentData.size < mPageSize){
                        data_enableLoadMore.postValue(false)
                    } else {
                        data_enableLoadMore.postValue(true)
                    }
                }
            } else {
                Log.e(TAG, "result0=" + userContent.msg)
                userContent.msg.let { sendHintMessage(it) }
            }

            //
            action_finishRefres.postValue(true)
            data_channelContent.postValue(data)
        }
    }

    fun onLoadMoreData(){
        mIsRefreshData = false
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            action_network_error.postValue(true)
        }
        request(onError) {
            val data = data_channelContent.value!!

            val userContent = Repo.http.getUserContent(mPageIndex, mPageSize, 0L)
            if(userContent.isSuccess()){
                mPageIndex++
                val userContentData:List<IndexContentBean> = userContent.data!!
                userContentData.forEach {
                    if(1 == it.state){
                        data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_NORMAL, null, it))
                    } else {
                        data.add(ChannelContentBean(ChannelContentBean.TYPE_ITEM_HINT, null, it))
                    }
                }
                //
                if(userContentData.size < mPageSize){
                    data_enableLoadMore.postValue(false)
                } else {
                    data_enableLoadMore.postValue(true)
                }

            } else {
                Log.e(TAG, "result0=" + userContent.msg)
                userContent.msg.let { sendHintMessage(it) }
            }
            //
            action_finishLoadMore.postValue(true)
            data_channelContent.postValue(data)
        }
    }

    fun onNetworkRetry(){
        if (mIsRefreshData) {
            onRefreshData()
        } else {
            onLoadMoreData()
        }
    }

    fun onLogout(){
        GoogleLoginManager.loginOut()
        FacebookLoginManager.loginOut()
        LineLoginManager.loginOut()
        //
        Repo.login.setLogin(null)
        Repo.user.setUser(null)
    }

    fun onClickCreate(){
        Repo.user.get()?.let { user ->
            if (user.isCreater()) {
                //web端发布介绍页
                action_toCreatorUploadPage.postValue(true)
            } else {
                //创造者引导页
                action_toCreatorPage.postValue(true)
            }
        }
    }

    fun onClickHistory(){
        action_toMyHistoryPage.postValue(true)
    }

    fun onClickSettings(){
        action_toSettingsPage.postValue(true)
    }

}